

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:huajianshe_app/base/AppNavigator.dart';
import 'package:huajianshe_app/base/BaseState.dart';
import 'package:huajianshe_app/common/App.dart';
import 'package:huajianshe_app/common/AppRouter.dart';
import 'package:huajianshe_app/common/load_image.dart';
import 'package:huajianshe_app/common/res.dart';
import 'package:huajianshe_app/constants/constant.dart';
import 'package:huajianshe_app/network/Api.dart';
import 'package:huajianshe_app/network/response/BaseResp.dart';
import 'package:huajianshe_app/pages/circle/CircleMemberListPage.dart';
import 'package:huajianshe_app/pages/circle/CircleStarDialog.dart';
import 'package:huajianshe_app/pages/circle/ContactAdminDialog.dart';
import 'package:huajianshe_app/pages/main/MomentItem.dart';
import 'package:huajianshe_app/pages/main/MomentListPage.dart';
import 'package:huajianshe_app/pages/search/SearchPage.dart';
import 'package:huajianshe_app/pages/user_info/LeaveMessageDialog.dart';
import 'package:huajianshe_app/pages/user_info/UserInfoWithButtonItem.dart';
import 'package:huajianshe_app/pages/user_info/model/UserModel.dart';
import 'package:huajianshe_app/utils/SpUtil.dart';
import 'package:huajianshe_app/utils/dialog_util.dart';
import 'package:huajianshe_app/utils/toast_utils.dart';
import 'package:huajianshe_app/widget/AvatarWidget.dart';
import 'package:huajianshe_app/widget/RoundedCornerWidget.dart';
import 'package:huajianshe_app/widget/RoundedUnderlineTabIndicator.dart';
import 'package:huajianshe_app/widget/SmartButton.dart';
import 'package:huajianshe_app/widget/StarWidget.dart';
import 'package:huajianshe_app/widget/appbar/CAppBar.dart';
import 'package:huajianshe_app/widget/dialog/YesOrNoDialog.dart';
import 'model/CircleModel.dart';
import 'package:im_flutter_sdk/im_flutter_sdk.dart';

//某个圈子的首页
class CircleDetailPage extends StatefulWidget {

  final int? circleId;
  final CircleDetailType circleDetailType;
  final CircleDetailData? circleDetailData;

  const CircleDetailPage(this.circleDetailType, this.circleId, {Key? key, this.circleDetailData}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _CircleDetailPageState();
  }
}

class _CircleDetailPageState extends BaseState<CircleDetailPage> with TickerProviderStateMixin {
  late SystemUiOverlayStyle overlayStyle;
  late double appbarOpacity;
  late ScrollController scrollController;
  late TabController tabControllerOuter;
  late TabController tabControllerInner;
  late int offset = 0;
  double membersHeight = 0;
  CircleDetailData? circleDetailData;
  late CircleDetailType type;

  @override
  void initState() {
    super.initState();
    type = widget.circleDetailType;
    overlayStyle = SystemUiOverlayStyle.light;
    appbarOpacity = 0;
    scrollController = ScrollController();
    tabControllerOuter = TabController(length: 2, vsync: this);
    tabControllerInner = TabController(length: 3, vsync: this);
    tabControllerOuter.addListener(() { onTabChanged(); });
    scrollController.addListener(() => {_onScroll()});
    print('${widget.circleDetailData}');
    if (widget.circleDetailData != null) {
      circleDetailData = widget.circleDetailData;
    } else {
      load();
    }
  }

  void load() {
    var data = { 'id': widget.circleId };
    sendReq(Api.circleDetail(), CircleDetailResp(), data: data);
  }

  @override
  void onReqSuccess<T>(T t, int code, String msg, String path) {
    super.onReqSuccess(t, code, msg, path);
    if (t is CircleDetailResp) {
      print('ok');
      circleDetailData = t.data;
      if (t.data?.isJoin != 1) {//如果是未加入，由于没有内页，自动切换到外页，省去麻烦
        type = CircleDetailType.outer;
      }
      setState(() => {});
    } else if (t is HxCreateAccountResp) {
      if (code == 0) {
        String myHxPwd = SpUtil.getString(Constant.KEY_IM_PWD);
        print('myHxPwd:$myHxPwd');
        if (myHxPwd == '') {
          print('saveHxId: ${t.data?.meHxId} ${t.data?.meHxPwd}');
          App.saveHxId(t.data?.meHxId, t.data?.meHxPwd);
        }
        circleDetailData?.hxGroupId = t.data?.hxGroupId;
        goToGroup(t.data?.hxGroupId);
      } else {
        Toast.show(msg);
      }
    } else if (t is BaseResp) {
      if (path.contains('Circle/joinCircle')) { //加入圈子
        if (t.code == 0) { //成功
          circleDetailData?.isJoin = 1;
          type = CircleDetailType.inner;
          setState(() => null);
        }
        Toast.show(msg);
      }
    }
  }

  void _onScroll() {
    // print('position ${controller.position} offset ${controller.offset}');
    int newOffset = scrollController.offset.toInt() - 50;
    if (newOffset > 50) newOffset = 50;
    if (newOffset < 0) newOffset = 0;
    if (offset == newOffset) return;
    offset = newOffset;
    this.appbarOpacity = offset.toDouble() * 2 * 0.01;
    print('appbarOpacity ${appbarOpacity} offset ${offset}');
    if (this.appbarOpacity > 0.5 && this.overlayStyle != SystemUiOverlayStyle.dark) {
      this.overlayStyle = SystemUiOverlayStyle.dark;
    }
    if (this.appbarOpacity <= 0.5 && this.overlayStyle != SystemUiOverlayStyle.light) {
      this.overlayStyle = SystemUiOverlayStyle.light;
    }
    setState(() { });
  }

  void onTabChanged() {
    print('onTabChanged');
    if (circleDetailData?.isJoin == 1 || type == CircleDetailType.inner) return;
    int index = tabControllerOuter.index;
    if (index == 1) {
      setState(() => tabControllerOuter.index = 0);
      showDialog(context: context, builder: (BuildContext context) {
        return YesOrNoDialog('此功能仅对圈内成员开放', yesText: '我要加入', onConfirm: join,);
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    Widget members = buildMembers();
    return
      Scaffold(
          body: Column(children: [
            Expanded(child: Stack(children: [
              NestedScrollView(
                controller: scrollController,
                body: buildTabBarView(),
                headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
                  return <Widget>[
                    SliverAppBar(
                      backgroundColor: Colors.white,
                      automaticallyImplyLeading: false,
                      // title: Text('2222'),
                      pinned: true,
                      elevation: 0,
                      expandedHeight: 470 + membersHeight,
                      flexibleSpace: FlexibleSpaceBar(
                          collapseMode: CollapseMode.pin,
                          background: Container( //头部整个背景颜色
                            height: double.infinity,
                            child: Stack(children: [
                              LoadImage('${circleDetailData?.background?.isNotEmpty == true ? circleDetailData?.background : 'bg_user_info'}', height: 175, width: double.infinity,), //todo
                              Column(children: <Widget>[
                                H(124),
                                SizedBox(width: double.infinity, child: Stack(children: [
                                  Padding(padding: EdgeInsets.only(top: 24), child: Row(mainAxisSize: MainAxisSize.max, children: <Widget>[
                                    Expanded(child:
                                    RoundedCornerWidget(width: double.infinity, height: 360 + membersHeight, backgroundColor: C.grayF6, borderColor: Colors.transparent, radius: 13, cornerPosition: 3,
                                      child: Column(mainAxisSize: MainAxisSize.max, crossAxisAlignment: CrossAxisAlignment.start, children: [
                                        ...buildCircleInfo(),
                                        H(15),
                                        buildScore(),
                                        H(10),
                                        members,
                                      ]),
                                    )
                                    )
                                  ])),
                                  //头像
                                  Positioned(left: 16, child: AvatarWidget(circleDetailData?.cover ?? '', 66, 3, 1))
                                ]))
                              ])
                            ])
                          ),
                        ),
                        bottom: buildTabBar()
                    )
                  ];
                },
              ),
              CAppBar(backImgUrl: 'ic_white_arrow', actions: [LoadImage('ic_white_share')]),
              Opacity(opacity: appbarOpacity, child: CAppBar(backImgUrl: 'ic_black_arrow', bgColor: Colors.white,
                actions: [LoadImage('ic_black_share')], title: circleDetailData?.title ?? '',))
            ])),
            buildBottom()
          ]),
          floatingActionButton: Padding(padding: EdgeInsets.only(bottom: 50),child:FloatingActionButton(
            backgroundColor: Colors.transparent,
            child: LoadImage('send_moment'),
            // child: Container(width: 52, height: 52, decoration: BoxDecoration(shape: BoxShape.circle, gradient:
            // LinearGradient(colors: [Color(0xFFFE2E2E), Color(0xFFF78C58)], begin: Alignment.topCenter, end: Alignment.bottomCenter),
            // ), child: Icon(Icons.add),),
            onPressed: () { AppNavigator.push(context, RouteName.PREPOST); },
          ))
      );
  }

  List<Widget> buildCircleInfo() {
    return [
      H(9),
      Row(mainAxisSize: MainAxisSize.max, children: [
        Spacer(),
        buildJoinButton(32, null),
        buildGotoInnerPageButton(),
        W(15)
      ]),
      H(10),
      Row(mainAxisSize: MainAxisSize.max, children: [
        W(13),
        Text(circleDetailData?.title ?? '', style: TS(17, C.gray333, fontWeight: FontWeight.bold)),
      ]),
      H(5),
      Row(mainAxisSize: MainAxisSize.max, children: [
        W(13),
        Text('创建时间', style: TS(12, C.gray666)),
        W(4),
        Text(circleDetailData?.createTime ?? '', style: TS(12, C.gray333))
      ]),
      H(7),
      Container(width: double.infinity, height: 45, alignment: Alignment.topLeft,
        child: Row(mainAxisSize: MainAxisSize.max, children: [
          W(13),
          Expanded(child: Text(circleDetailData?.desc ?? '', style: TS(12, C.gray666), maxLines: 2, overflow: TextOverflow.ellipsis,)),
          W(13)
        ])
      ),
      Row(crossAxisAlignment: CrossAxisAlignment.start, children: [
        W(13),
        Container(alignment: Alignment.centerLeft, width: 95, child: Column(mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.end, children: [
          Row(mainAxisSize: MainAxisSize.min, children: [
            Text('${circleDetailData?.momentNum ?? 0}', style: TS(21, C.gray333, fontWeight: FontWeight.bold)),
            Text('条', style: TS(12, C.gray999))
          ]),
          H(0),
          Text('动态', style: TS(12, C.gray999))
        ])),
        Container(alignment: Alignment.centerLeft, width: 95, child: Column(mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.end, children: [
          Row(mainAxisSize: MainAxisSize.min, children: [
            Text('${circleDetailData?.businessNum ?? 0}', style: TS(21, C.gray333, fontWeight: FontWeight.bold)),
            Text('条', style: TS(12, C.gray999))
          ]),
          H(0),
          Text('商机', style: TS(12, C.gray999))
        ])),
        Container(alignment: Alignment.centerLeft, width: 95, child: Column(mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.end, children: [
          Row(mainAxisSize: MainAxisSize.min, children: [
            Text('${circleDetailData?.memberNum ?? 0}', style: TS(21, C.gray333, fontWeight: FontWeight.bold)),
            Text('人', style: TS(12, C.gray999))
          ]),
          H(0),
          Text('成员', style: TS(12, C.gray999))
        ])),
      ])
    ];
  }

  Widget buildScore() {
    return
      Padding(padding: EdgeInsets.symmetric(horizontal: 13),
          child: RoundedCornerWidget(height: 96, padding: EdgeInsets.symmetric(horizontal: 13),
            child: Row(crossAxisAlignment: CrossAxisAlignment.start, children: [
              Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
                H(10),
                Text('圈子评分', style: TS(15, C.gray333, fontWeight: FontWeight.bold)),
                H(4),
                Row(mainAxisSize: MainAxisSize.min, children: [
                  Text('${circleDetailData?.totalScore ?? 0}', style: TS(21, C.primary)),
                  StarWidget(5, 9, 4, false, current: ValueNotifier(circleDetailData?.totalScore ?? 0))
                  // Text('分', style: TS(12, C.gray999))
                ]),
                H(0),
                circleDetailData?.isCaptain == 1 ? Padding(padding: EdgeInsets.only(top: 5), child: Text('不能给自己的圈子打分', style: TS(13, C.primary))) :
                (circleDetailData?.isJoin == 1 && circleDetailData?.isScore == 1) ? Padding(padding: EdgeInsets.only(top: 5), child: Text('已打分', style: TS(13, C.primary))) :
                  Padding(padding: EdgeInsets.only(top: 5), //未进圈子或者进了但没打过分
                      child: SmartButton(text: '我要打分', fontSize: 13, textColor: C.primary, backgroundColor: Colors.transparent,
                        width: 72, height: 20, radius: 11, onPressed: score,)
                  ),
              ]),

              Spacer(),
              Column(mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.end, children: [
                H(12),
                if ((circleDetailData?.labelScore?.length ?? 0) > 0)
                  RoundedCornerWidget(child: Row(mainAxisSize: MainAxisSize.min, children: [
                    Text('${circleDetailData?.labelScore?[0].name ?? ''}', style: TS(11, C.gray666)),
                    W(10),
                    Text('${circleDetailData?.labelScore?[0].score ?? 0}分', style: TS(11, C.primary),)
                  ]),
                  backgroundColor: C.grayF6, borderWidth: 0, radius: 2, padding: EdgeInsets.symmetric(horizontal: 7, vertical: 2),),
                H(8),
                if ((circleDetailData?.labelScore?.length ?? 0) > 1)
                  RoundedCornerWidget(child: Row(mainAxisSize: MainAxisSize.min, children: [
                    Text('${circleDetailData?.labelScore?[1].name ?? ''}', style: TS(11, C.gray666)),
                    W(10),
                    Text('${circleDetailData?.labelScore?[1].score ?? 0}分', style: TS(11, C.primary),)
                  ]),
                    backgroundColor: C.grayF6, borderWidth: 0, radius: 2, padding: EdgeInsets.symmetric(horizontal: 7, vertical: 2),),
                H(8),
                if ((circleDetailData?.labelScore?.length ?? 0) > 2)
                  RoundedCornerWidget(child: Row(mainAxisSize: MainAxisSize.min, children: [
                    Text('${circleDetailData?.labelScore?[2].name ?? ''}', style: TS(11, C.gray666)),
                    W(10),
                    Text('${circleDetailData?.labelScore?[2].score ?? 0}分', style: TS(11, C.primary),)
                  ]),
                    backgroundColor: C.grayF6, borderWidth: 0, radius: 2, padding: EdgeInsets.symmetric(horizontal: 7, vertical: 2),)
              ],)
             ]), backgroundColor: Colors.white, radius: 13,
          )
      );
  }

  Widget buildMembers() {
    if (type == CircleDetailType.inner) {
      membersHeight = 0;
      return H(0);
    }
    membersHeight = 47;
    List<Widget> result = [];

    var circleMembers = circleDetailData?.circleMembers;

    if (circleMembers?.isNotEmpty == true) {
      print('${circleMembers!.length}');
      // membersHeight += 60 * circleMembers.length + 1 * (circleMembers.length - 1);
      CircleInfo? circleInfo = circleDetailData?.isJoin == 1 ? null :
      (CircleInfo()
        ..circleId = widget.circleId
        ..title = circleDetailData?.title
      );
      for (int i = 0; i < circleMembers.length; i++) {
        if (i > 0) {
          result.add(DD(paddingLeft: 13, paddingRight: 13,));
          membersHeight += 1;
        }
        result.add(UserInfoItemWithButton(userInfo: circleMembers[i].userInfo, circleInfo: circleInfo, buttonText: '洽谈',
          padding: EdgeInsets.symmetric(horizontal: 13, vertical: 10),));
        membersHeight += 60;
      }
    } else {
      membersHeight += 132;
      result.add(Container(width: double.infinity, height: 132, child: Center(
        child: Column(mainAxisSize: MainAxisSize.min, children: [
          Text('还没有成员加入，快去邀请好友加入', style: TS(13, C.gray666)),
          H(3),
          Text('或者分享给朋友邀请加入吧！', style: TS(13, C.gray666)),
          H(14),
          SmartButton(text: '去分享', fontSize: 15, textColor: Colors.white, width: 150, height: 40, radius: 20, backgroundColor: C.primary,)
        ],),
      )));
      // return result;
    }
    return
        RoundedCornerWidget(height: membersHeight, backgroundColor: Colors.white, borderWidth: 0, radius: 13,
            child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
              Container(height: 44,
                  child: GestureDetector(onTap: () { moreMembers(); },
                      child: Row(children: [
                        W(13),
                        Text('活跃成员', style: TS(15, C.gray333, fontWeight: FontWeight.bold),),
                        Spacer(),
                        SmartButton(text: '更多', fontSize: 14, textColor: C.gray999, backgroundColor: Colors.transparent,
                          width: 65, height: 20, drawableRight: LoadImage('ic_gray_right_arrow'), drawablePadding: 5, borderColor: Colors.transparent,
                        ),
                        W(13)
                      ])
                  )
              ),
              Padding(padding: EdgeInsets.only(left: 27), child: ColoredBox(color: C.primary, child: SizedBox(width: 33, height: 2))),
              D(),
              ...result,
            ])
        );
  }

  SmartButton buildJoinButton(double height, double? width, ) {
    if (circleDetailData?.isCaptain == 1 && widget.circleDetailType == CircleDetailType.inner) {//仅当内页且是圈主才有管理
      return SmartButton(text: '管理圈子', fontSize: 12, textColor: Colors.white, height: height, width: width, radius: height / 2,
        drawableLeft: LoadImage('ic_management'), drawablePadding: 4, padding: EdgeInsets.symmetric(horizontal: 12), onPressed: manage,);
    } else if (circleDetailData?.isJoin == 1) {
      return SmartButton(text: '进入群聊', fontSize: 12, textColor: Colors.white, height: height, width: width, radius: height / 2,
        drawableLeft: LoadImage('ic_message'), drawablePadding: 4, padding: EdgeInsets.symmetric(horizontal: 12),onPressed: group,);
    } else {
      return SmartButton(text: '¥${circleDetailData?.price}加入圈子', fontSize: 12, textColor: Colors.white, height: height, width: width, radius: height / 2,
        drawableLeft: null, drawablePadding: 0, padding: EdgeInsets.symmetric(horizontal: 12), onPressed: join,);
    }
  }

  Widget buildGotoInnerPageButton() {
    if (circleDetailData?.isJoin == 1 && widget.circleDetailType == CircleDetailType.inner) {
      return 
          Padding(padding: EdgeInsets.only(left: 6),
            child: SmartButton(width: 45, height: 32, radius: 16, borderColor: C.gray999, borderWidth: 1, backgroundColor: Colors.transparent,
            drawableLeft: LoadImage('ic_inner_circle'), onPressed: () => goOuter(),)
          );
    }
    return W(0);
  }

  Widget buildBottom() {
    if (circleDetailData?.isJoin == 1) return H(0);
    return
        Container(width: double.infinity, height: 55, child: Row(children: [
          W(20),
          SmartButton(width: 66, height: 38, text: '咨询管理员', textColor: C.gray333, fontSize: 13, backgroundColor: Colors.transparent, borderColor: Colors.transparent,
            drawableTop: LoadImage('ic_contact_admin'), onPressed: contactAdmin,),
          Spacer(),
          SmartButton(width: 210, height: 44, radius: 22, text: '¥${circleDetailData?.price}加入圈子', textColor: Colors.white, fontSize: 15,
            onPressed: () => join(),),
          W(8)
        ]), decoration: BoxDecoration(
            color: Colors.white,
            boxShadow: [
              BoxShadow(color: Color.fromRGBO(0, 0, 0, 0.08), blurRadius: 11.0, spreadRadius: 0.0, offset: Offset(1, 0)),
            ]
        ));
  }

  PreferredSizeWidget buildTabBar() {
    if (type == CircleDetailType.outer) {
      return
        PreferredSize(preferredSize: Size.fromHeight(32), //40
            child: ColoredBox(color: Colors.white, child: Align(alignment: Alignment.centerLeft,
              child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
                H(10),
                Padding(padding: EdgeInsets.only(left: 8, top: 2), child: TabBar(
                    labelPadding: EdgeInsets.only(left: 6, right: 6, bottom: 3),
                    indicatorWeight: 0,
                    indicatorSize: TabBarIndicatorSize.label,
                    indicator: RoundedUnderlineTabIndicator(
                      insets: EdgeInsets.only(left: 0, top: 0, bottom: -7),
                      borderSide: BorderSide(width: 1, color: C.primary),
                    ),
                    isScrollable: true,
                    controller: tabControllerOuter,
                    labelColor: C.gray333,
                    unselectedLabelColor: C.gray999,
                    labelStyle: TS(15, C.gray333, fontWeight: FontWeight.bold),
                    unselectedLabelStyle: TS(14, C.gray999),
                    tabs: [Text('公开动态(${circleDetailData?.publicNum}条)'),
                      GestureDetector(onTap: null, child: Text('圈内可见动态(${circleDetailData?.privateNum}条)'))]
                )),
                H(7),
                D()
              ]),
            ))
        );
    } else {
      return
        PreferredSize(preferredSize: Size.fromHeight(32),
          child: RoundedCornerWidget(child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
            H(8),
            TabBar(
                labelPadding: EdgeInsets.only(left: 0, right: 24, bottom: 3),
                indicatorWeight: 0,
                indicatorSize: TabBarIndicatorSize.label,
                indicator: RoundedUnderlineTabIndicator(
                  insets: EdgeInsets.only(left: -14, right: 10, top: 0, bottom: -5),
                  borderSide: BorderSide(width: 1, color: C.primary),
                ),
                isScrollable: true,
                controller: tabControllerInner,
                labelColor: C.gray333,
                unselectedLabelColor: C.gray999,
                labelStyle: TS(15, C.gray333, fontWeight: FontWeight.bold),
                unselectedLabelStyle: TS(15, C.gray999),
                tabs: [Text('推荐'), Text('商机'), Text('成员')]
            ),
            H(5),
            D(),
          ]), backgroundColor: Colors.white, radius: 13, cornerPosition: 3, height: 44, width: double.infinity,
          padding: EdgeInsets.symmetric(horizontal: 13),)
        );

    }
  }

  Widget buildTabBarView() {
    if (type == CircleDetailType.outer) {
      return
        TabBarView(physics: circleDetailData?.isJoin == 1 ? null : NeverScrollableScrollPhysics(), /*禁止左右滑动*/
          controller: tabControllerOuter, children: [
            Padding(padding: EdgeInsets.only(top: 10), child: MomentListPage(momentType: MomentType.MOMENT_TYPE_PUBLIC_IN_CIRCLE,
                circleId: widget.circleId, initNotShowProgress: true)),
            Padding(padding: EdgeInsets.only(top: 10), child: MomentListPage(momentType: MomentType.MOMENT_TYPE_ALL_IN_CIRCLE,
                circleId: widget.circleId, initNotShowProgress: true)),
          ]);
    } else {
      return
        TabBarView(controller: tabControllerInner, children: [
          buildTab ('输入感兴趣的内容~', () => { AppNavigator.push(context, RouteName.SEARCH, arguments: {
              'searchType': SearchType.moment,
              'momentType': MomentType.MOMENT_TYPE_ALL_IN_CIRCLE,
              'circleId': widget.circleId,
              'businessType': MomentBusinessType.MOMENT
            })
          }, MomentListPage(momentType: MomentType.MOMENT_TYPE_ALL_IN_CIRCLE, circleId: widget.circleId, businessType: MomentBusinessType.MOMENT), true),
          buildTab ('输入感兴趣的内容~', () => { AppNavigator.push(context, RouteName.SEARCH, arguments: {
              'searchType': SearchType.moment,
              'momentType': MomentType.MOMENT_TYPE_ALL_IN_CIRCLE,
              'circleId': widget.circleId,
              'businessType': MomentBusinessType.OPPORTUNITY
            })
          }, MomentListPage(momentType: MomentType.MOMENT_TYPE_ALL_IN_CIRCLE, circleId: widget.circleId, businessType: MomentBusinessType.OPPORTUNITY), true),
          buildTab ('搜索想要找的人', () => { AppNavigator.push(context, RouteName.SEARCH, arguments: {
              'circleId': widget.circleId,
            })
          }, CircleMemberListPage(widget.circleId), false),
        ]);
    }
  }

  Widget buildTab(String hint, Function onClick, Widget child, bool padding) {
    return
      Column(children: [
        ColoredBox(color: Colors.white, child: GestureDetector(onTap: () => onClick(),
            child: Padding(padding: EdgeInsets.only(left: 10, right: 10, top: 5, bottom: 10),
                child: RoundedCornerWidget(width: double.infinity, height: 38,
                  child: Row(children: [
                    W(10),
                    LoadImage('ic_gray_search'),
                    W(3),
                    Text(hint, style: TS(13, C.gray666))
                  ]), backgroundColor: C.grayF6, radius: 7, borderColor: C.grayDDD,
                )
            )
        )),
        if (padding) H(10),
        Expanded(child: child)
      ]);
  }

  score() { //去打分
    if (circleDetailData?.isJoin == 1) {
      DialogUtil.showBottomDialogV2(context, CircleStarDialog(widget.circleId, circleDetailData?.title, circleDetailData?.labelScore),
        height: 0.8, radius: 13, bgColor: Colors.transparent, margin: EdgeInsets.symmetric(horizontal: 5));
    } else {
      join();
    }
  }

  manage() { //管理圈子
    AppNavigator.push(context, RouteName.CIRCLE_MANAGEMENT, arguments: {'circleId': circleDetailData?.id, 'name': circleDetailData?.title, 'viewNum': circleDetailData?.viewNum,
      'score': circleDetailData?.totalScore, 'importUrl': circleDetailData?.importUrl});
  }

  goOuter() {//从内页切换到外页
    AppNavigator.push(context, RouteName.CIRCLE_DETAIL, arguments: {'circleDetailType': CircleDetailType.outer,
      'circleId': widget.circleId, 'circleDetailData': circleDetailData});
  }

  moreMembers() {}

  contactAdmin() {
    print('contactAdmin');
    showDialog(context: context, builder: (BuildContext context) {
      return ContactAdminDialog('${circleDetailData?.masterWechat}');
    });
  } //联系管理员

  join() {//加圈子
    if (circleDetailData?.priceType == 2) { // 免费圈子直接加入
      sendReq(Api.circleJoin(), BaseResp(), data: {'circle_id': widget.circleId}, doNotShowProgressBar: false);
    } else {
      AppNavigator.push(context, RouteName.PAY, arguments: {
        'url': circleDetailData?.cover,
        'title': circleDetailData?.title,
        'desc': circleDetailData?.desc,
        'price': circleDetailData?.price
      });
    }
  }

  group() { //进入群聊
    String myHxPwd = SpUtil.getString(Constant.KEY_IM_PWD);
    String hxGroupId = circleDetailData?.hxGroupId ?? '';
    if (myHxPwd != '' && hxGroupId != '') {
      print('环信id都有，走起$hxGroupId');
      goToGroup(hxGroupId);
    } else {
      sendReq(Api.getHxGroupId(), HxCreateAccountResp(), data: {'circle_id': widget.circleId});
    }
  }

  goToGroup(String? hxGroupId) async {
    EMConversation conv = await EMClient.getInstance.chatManager.getConversation(hxGroupId, EMConversationType.GroupChat);
    AppNavigator.push(context, RouteName.CHAT, arguments: {'id': circleDetailData?.title, 'conv': conv});
  }

}

enum CircleDetailType {//这页面有2种形态，内页和外页，默认进内页，有个按钮跳转到外页，如果未加入，默认进外页
  inner, outer
}